<template>
    <a-modal :confirm-loading="modalOption.loading" :okText="modalOption.okText" @ok="modalOption.onOk"
             @cancel="modalOption.onCancel" :cancelText="modalOption.cancelText" :width="modalOption.width" :visible="visible"
             :title="modalOption.title">
        <div class="container">
            <a-form-model ref="formModel" :label-col="formOption.labelCol" :wrapper-col="formOption.wrapperCol"
                          :layout="formOption.layout" :label-align="formOption.labelAlign" :hide-required-mark="formOption.hideRequiredMark"
                          :rules="formOption.rules" :model="formOption.formData">
                <div class="tool-bar">
                    <a-card hoverable title="产品">
                        <a-row class="table">
                            <a-col class="col-h" :span="8">
                                <a-form-model-item label="产品类别" prop="type">
                                    <a-select :disabled="true" v-model="formOption.formData.type" placeholder="请选择产品类别" @change="typeChange">
                                        <a-select-option value="成品">成品</a-select-option>
                                        <a-select-option value="待包装成品">待包装成品</a-select-option>
                                        <a-select-option value="半成品">半成品</a-select-option>
                                    </a-select>
                                </a-form-model-item>
                            </a-col>
                            <a-col class="col-h" :span="8">
                                <a-form-model-item label="产品分类" prop="materialType">
                                    <a-select :disabled="true" v-model="formOption.formData.materialType" placeholder="请选择产品分类" @change="materialTypeChange">
                                        <a-select-option v-for="item in materialTypeList" :key="item.value" :value="item.value">{{item.name}}</a-select-option>
                                    </a-select>
                                </a-form-model-item>
                            </a-col>
                            <a-col class="col-h" :span="8">
                                <a-form-model-item  label="标准" prop="standard">
                                    <a-select :disabled="viewDetail" v-model="formOption.formData.standard" placeholder="请选择标准" @change="standardChange">
                                        <a-select-option value="国标">国标</a-select-option>
                                        <a-select-option value="省标">省标</a-select-option>
                                        <a-select-option value="省标-北京">省标-北京</a-select-option>
                                        <a-select-option value="省标-上海">省标-上海</a-select-option>
                                        <a-select-option v-if="type=='半成品' || type=='待包装成品'" value="其他">其他</a-select-option>
                                    </a-select>
                                </a-form-model-item>
                            </a-col>
                        </a-row>
                        <a-row class="table">
                            <a-col class="col-h" :span="8" v-if="formOption.formData.materialType ==='配方颗粒(颗粒)'">
                                <a-form-model-item label="产品大类" prop="inventoryType">
                                    <a-select v-if="formOption.formData.standard" :disabled="viewDetail" v-model="formOption.formData.inventoryType" placeholder="请选择产品大类">
                                        <a-select-option v-if="formOption.formData.standard == '国标'" value="C60">C60</a-select-option>
                                        <a-select-option v-if="formOption.formData.standard == '国标'" value="C70">C70</a-select-option>
                                        <a-select-option v-if="formOption.formData.standard == '省标'" value="C80">C80</a-select-option>
                                        <a-select-option v-if="formOption.formData.standard == '省标'" value="C90">C90</a-select-option>
                                        <a-select-option v-if="formOption.formData.standard == '省标-北京'" value="C81">C81</a-select-option>
                                        <a-select-option v-if="formOption.formData.standard == '省标-上海'" value="C82">C82</a-select-option>
                                        <a-select-option v-if="formOption.formData.standard == '省标-北京'" value="C91">C91</a-select-option>
                                        <a-select-option v-if="formOption.formData.standard == '省标-上海'" value="C92">C92</a-select-option>
                                    </a-select>
                                    <span v-else>请选择标准</span>
                                </a-form-model-item>
                            </a-col>
                            <a-col class="col-h" :span="8">
                                <a-form-model-item label="产品名称" prop="name">
                                    <a-input :disabled="viewDetail" v-model="formOption.formData.name" placeholder="请输入产品名称" />
                                </a-form-model-item>
                            </a-col>
                            <a-col class="col-h" :span="8" v-if="formOption.formData.materialType ==='配方颗粒(颗粒)'">
                                <a-form-model-item  label="规格" prop="spec">
                                    <a-select :disabled="viewDetail" v-model="formOption.formData.spec" placeholder="请选择产品规格" @change="calcDay">
                                        <a-select-option value="大袋">大袋</a-select-option>
                                        <a-select-option value="小袋">小袋</a-select-option>
                                    </a-select>
                                </a-form-model-item>
                            </a-col>
                        </a-row>
                        <a-row class="table">
                            <a-col class="col-h" :span="8" v-if="formOption.formData.materialType ==='配方颗粒(颗粒)'">
                                <a-form-model-item  label="装量" prop="loadingQuantity">
                                    <a-input :disabled="viewDetail" v-model="formOption.formData.loadingQuantity" placeholder="请输入装量" suffix="g/袋"/>
                                </a-form-model-item>
                            </a-col>
                            <a-col class="col-h" :span="8">
                                <a-form-model-item :label="type=='成品'?'保质期':'有效期'" prop="validDay">
                                    <a-input :disabled="viewDetail" v-model="formOption.formData.validDay" :placeholder="`请输入${type=='成品'?'保质期':'有效期'}`" suffix="月"/>
                                </a-form-model-item>
                            </a-col>
                            <a-col class="col-h" :span="8">
                                <a-form-model-item label="产品代码" prop="code">
                                    <a-input :disabled="viewDetail" v-model="formOption.formData.code" placeholder="请输入产品代码" />
                                </a-form-model-item>
                            </a-col>
                        </a-row>
                        <a-row class="table">
                            <a-col class="col-h" :span="8">
                                <a-form-model-item  label="状态" prop="state">
                                    <a-radio-group :disabled="viewDetail" v-model="formOption.formData.state">
                                        <a-radio :value="false">禁用</a-radio>
                                        <a-radio :value="true">启用</a-radio>
                                    </a-radio-group>
                                </a-form-model-item>
                            </a-col>
                            <a-col class="col-h" :span="8" v-if="formOption.formData.materialType ==='配方颗粒(颗粒)'">
                                <a-form-model-item  :label-col="{style:{width:'125px'}}" label="季度销售需求量" prop="quarterSell">
                                    <a-input :disabled="viewDetail" v-model="formOption.formData.quarterSell" @change="calcDayRequirement" placeholder="请输入季度销售需求量" suffix="袋"/>
                                </a-form-model-item>
                            </a-col>
                            <a-col class="col-h" :span="8" v-if="formOption.formData.materialType ==='配方颗粒(颗粒)'">
                                <a-form-model-item  :label-col="{style:{width:'125px'}}" label="计划包装量" prop="planNum">
                                    <a-input :disabled="true" v-model="formOption.formData.planNum" placeholder="请输入计划包装量" suffix="袋"/>
                                </a-form-model-item>
                            </a-col>
                        </a-row>
                        <a-row class="table">
                            <a-col class="col-h" :span="8" v-if="formOption.formData.materialType ==='配方颗粒(颗粒)'">
                                <a-form-model-item  label="每日需求量">
                                <span v-if="formOption.formData.dayRequirement" style="font-weight: 500;font-size: 20px;color:red">
                                    {{formOption.formData.dayRequirement}}
                                    <span style="margin-left: 3px;color:initial">袋</span>
                                </span>
                                <span v-else>请输入季度销售需求量</span>
                                </a-form-model-item>
                            </a-col>
                            <a-col class="col-h" :span="8" v-if="formOption.formData.materialType ==='配方颗粒(颗粒)'">
                                <a-form-model-item  label="包装后总天数">
                                <span v-if="formOption.formData.packDay" style="font-weight: 500;font-size: 20px;color:red">
                                    {{formOption.formData.packDay}}
                                    <span style="margin-left: 3px;color:initial">天</span>
                                </span>
                                    <span v-else>请填写规格和季度销售需求量</span>
                                </a-form-model-item>
                            </a-col>
                            <a-col class="col-h" :span="8" v-if="formOption.formData.materialType ==='配方颗粒(颗粒)'">
                                <a-form-model-item  label="上市备案号">
                                    <a-input :disabled="viewDetail" v-model="formOption.formData.prodRecordCode" placeholder="上市备案号"></a-input>
                                </a-form-model-item>
                            </a-col>
                            <a-col v-if="type !='成品'" class="col-h" :span="8">
                                <a-form-model-item label="复验期(月)" prop="reInspection">
                                    <a-input-number style="width: 220px" :disabled="viewDetail" v-model="formOption.formData.reInspection" placeholder="请输入复验期(月)" />
                                </a-form-model-item>
                            </a-col>
                            <a-col v-if="type !='成品'" class="col-h" :span="8">
                                <a-form-model-item label="复验期提醒天数/天" prop="reInspectionNoticeDay">
                                    <a-input-number style="width: 220px" :disabled="viewDetail" v-model="formOption.formData.reInspectionNoticeDay" placeholder="请输入复验期提醒天数/天" />
                                </a-form-model-item>
                            </a-col>
                        </a-row>
                    </a-card>
                    <a-row class="table">
                        <a-col>
                            <a-card hoverable title="BOM配置">
                                <a-row class="table">
                                    <a-col class="col-h" :span="8">
                                        <a-icon type="question-circle" style="color: #f63109" />
                                        <a-popover title="投料需求计算">
                                            <template slot="content">
                                                <p>该投料为{{formOption.formData.materialType}},请先确认对应品种{{formOption.formData.materialType}}是否添加</p>
                                            </template>
                                            <a-form-model-item  :label-col="{ style: { width: '100px' } }" label="选择投料" prop="materialName">
                                                <a-select show-search :filterOption="$filterOption" :disabled="viewDetail" v-if="formOption.formData.materialType" v-model="formOption.formData.materialName" placeholder="请选择投料">
                                                    <a-select-option v-for="(item,i) in materialList" :key="i" :value="item.name">{{item.name}}</a-select-option>
                                                </a-select>
                                                <span v-else>请先选择产品分类</span>
                                            </a-form-model-item>
                                        </a-popover>
                                    </a-col>
                                    <a-col class="col-h" :span="12">
                                        <a-icon type="question-circle" style="color: #999" />
                                        <a-popover title="投料需求计算">
                                            <template slot="content">
                                                <p style="width: 300px;">
                                                    <p>半成品颗粒需求量 =（小袋包装计划量*小袋装量+大袋包装计划量*大袋装量）/ <span style="color:red">99%</span></p>
                                                    <p>半成品提取物需求量 = 半成品颗粒需求量 / <span style="color:red">97%</span></p>
                                                    <p>提取饮片需求量 = 半成品提取物需求量 / 制成量</p>
                                                    <p>药材需求量=提取饮片需求量 / 饮片收率</p>
                                                </p>
                                            </template>
                                            <a-form-model-item label-align="left" :label-col="{style:{width:'240px'}}" :label="'投料需求:'+investmentLabel" prop="bomNum">
                                                <a-input :disabled="viewDetail" v-model="formOption.formData.bomNum" placeholder="请输入投料需求" suffix="%"></a-input>
                                            </a-form-model-item>
                                        </a-popover>
                                    </a-col>
                                </a-row>
                                <a-row class="table">
                                   <a-col :span="8" v-if="formOption.formData.materialType == '饮片'">
                                       <a-form-model-item label="提取方式" prop="tqWay">
                                           <div v-for="(item,i) in formOption.formData.tqWay" :key="i">
                                               <a-input style="width: 150px;margin-right: 5px" v-model="item.value" placeholder="请输入提取方式"></a-input>
                                               <a-button v-if="i!==0" type="danger" @click="deleteTqWay(i)" :disabled="viewDetail">删除</a-button>
                                               <a-button v-if="i===0" type="primary" @click="addTqWay" :disabled="viewDetail">新增</a-button>
                                           </div>
                                       </a-form-model-item>
                                   </a-col>
                                    <a-col :span="8" v-if="formOption.formData.materialType == '中药材'">
                                        <a-form-model-item label="前处理工艺流程" prop="tech">
                                            <div v-for="(item,i) in formOption.formData.tech" :key="i">
                                                <a-input style="width: 150px;margin-right: 5px" v-model="item.value" placeholder="请输入前处理工艺流程"></a-input>
                                                <a-button v-if="i!==0" type="danger" @click="deleteTech(i)" :disabled="viewDetail">删除</a-button>
                                                <a-button v-if="i===0" type="primary" @click="addTech" :disabled="viewDetail">新增</a-button>
                                            </div>
                                        </a-form-model-item>
                                    </a-col>
                                    <a-col :span="8" v-if="formOption.formData.materialType == '饮片' || formOption.formData.materialType == '中药材'">
                                        <a-form-model-item :label-col="{style:{width:'94px'}}" label="投料批量" prop="inputBatch">
                                            <div v-for="(item,i) in formOption.formData.inputBatch" :key="i">
                                                <a-input style="width: 150px;margin-right: 5px" v-model="item.value" placeholder="投料批量" suffix="kg/批"></a-input>
                                                <a-button v-if="i!==0" type="danger" @click="deleteInputBatch(i)" :disabled="viewDetail">删除</a-button>
                                                <a-button v-if="i===0" type="primary" @click="addInputBatch" :disabled="viewDetail">新增</a-button>
                                            </div>
                                        </a-form-model-item>
                                    </a-col>
                                    <a-col :span="8" v-if="formOption.formData.materialType == '饮片' || formOption.formData.materialType == '中药材'">
                                        <a-form-model-item label="工艺注意事项" prop="notice">
                                            <a-input class="inputs" v-model="formOption.formData.notice" placeholder="请输入工艺注意事项"></a-input>
                                        </a-form-model-item>
                                    </a-col>
                                </a-row>
                                <a-row class="table">
                                    <a-col :span="18">
                                        <a-row class="table" v-for="(item,i) in formOption.formData.stockConfig" :key="i">
                                            <a-col :span="10">
                                                <a-form-model-item label="可销售天数" prop="stockConfig">
                                                    <a-input-group compact>
                                                        <a-input-number :disabled="viewDetail" :min="0" v-model="item.start"/>
                                                        <a-input-number placeholder="~" style=" width: 31px; border-left: 0; pointer-events: none; backgroundColor: #fff" />
                                                        <a-input-number :disabled="viewDetail" style="border-left: 0" :min="0" v-model="item.end"/>
                                                    </a-input-group>
                                                </a-form-model-item>
                                                <span style="line-height: 40px">天</span>
                                            </a-col>
                                            <a-col :span="10">
                                                <span :style="{color:item.level === '高' ? '#F76A4A':item.level === '中' ? '#FFA842':'#22B585',}" style="line-height: 40px;">紧急程度：{{item.level}}</span>
                                            </a-col>
                                        </a-row>
                                    </a-col>
                                    <a-col :span="5" v-if="formOption.formData.materialType === '配方颗粒(颗粒)' || formOption.formData.materialType=== '饮片'">
                                        <a-form-model-item v-if="modalOption.pageReady" label="上传图片" prop="img">
                                            <img v-if="viewDetail" style="width: 250px;" :src="formOption.formData.img"/>
                                            <Upload v-else :uploadLength="1" :defImgList="[formOption.formData.img]" @uploadChange="imgUpload"></Upload>
                                        </a-form-model-item>
                                    </a-col>
                                </a-row>
                            </a-card>
                        </a-col>
                    </a-row>
                </div>
            </a-form-model>
        </div>
    </a-modal>
</template>
<script lang="ts">
import { Vue, Component, Prop, Watch } from "vue-property-decorator";
import nextApi from "@/api/nextApi";
import {t} from "@wangeditor/editor";
@Component({
    name: "addOrEdit",
    methods: {t},
    components: {
        Upload: () => import("@/components/upload.vue"),
    },
})
export default class extends Vue {
    @Prop({ default: false }) visible!: boolean;
    @Prop({ default: "" }) editId!: string;
    @Prop({ default: false }) viewDetail!: boolean;
    @Prop({default: ""}) type!: string;
    @Prop({default:""}) materialType!: string;
    // 正确位置：定义后再赋值
    $filterOption: any;

    constructor() {
        super();
        this.$filterOption = this.$filterOption; // 可以在构造函数中赋值
    }

    @Watch("visible")
    visibleChange(val) {
        if (val) {
            setTimeout(()=>{
                this.modalOption.pageReady = true
                this.formOption.formData.type = this.type
                console.log(this.materialType)
                if(this.type == '成品'){
                    this.formOption.formData.materialType = '配方颗粒(颗粒)'
                }else if(this.type == '待包装成品'){
                    this.formOption.formData.materialType = '提取物'
                }else{
                    this.formOption.formData.type = '半成品'
                    this.formOption.formData.materialType = this.materialType === "提取物" ? '饮片':'中药材'
                }
                this.materialTypeChange()
                this.changeRule()
            },500)
        }else{
            this.modalOption.pageReady = false
            this.modalOption.title = "新增";
            (this.$refs.formModel as any).resetFields();
            this.formOption.formData = {
                planNum:'0',
                reInspection:'',//12.2新加字段复验期
                reInspectionNoticeDay:'',//12.2新加字段复验期提醒天数
                name: "",
                code: "",
                materialType: "",
                type: "成品",
                validDay: "",
                inventoryType:'',
                materialName:'',
                img: "",
                state: true,
                standard:"国标",
                dayRequirement: "",
                bomNum: "",
                stockConfig:[
                    {
                        start: "",
                        end: "",
                        level: "高",
                    },
                    {
                        start: "",
                        end: "",
                        level: "中",
                    },
                    {
                        start: "",
                        end: "",
                        level: "低",
                    },
                ],
                tqWay:[{value:''}],
                inputBatch:[{value:''}],
                tech:[{value:''}],
                notice:''
            }
        }
    }
    @Watch("editId")
    editIdChange(val) {
        if (val) {
            console.log(val,'id')
            console.log('是否是预览：',this.viewDetail)
            if(val && this.viewDetail){
                this.modalOption.title = "详情";
            }else{
                this.modalOption.title = "编辑";
            }
            //查询当前id的数据
            this.getInfo()
        }
    }
    materialTypeList:any = [
        {
            name: "配方颗粒",
            value: "配方颗粒(颗粒)",
        },
        {
            name: "配方颗粒(颗粒)",
            value: "提取物",
        },
        {
            name: "提取物",
            value: "饮片",
        },
        {
            name: "提取饮片",
            value: "中药材",
        },
    ]
    //组件配置
    modalOption: any = {
        pageReady: false,
        width: "90%",
        title: "新增",
        okText: "保存",
        cancelText: "取消",
        loading: false,
        onOk: () => {
            this.modalOk();
        },
        onCancel: () => {
            this.closeModal();
        },
    };
    //表单配置
    formOption: any = {
        labelCol: { style: { width: "125px" } },
        wrapperCol: { style: { width: "220px" } },
        layout: "inline",
        labelAlign: "right",
        hideRequiredMark: false,
        formData: {
            loadingQuantity:'',
            packDay:'',//2025.3.3新增字段 包装后总天数
            planNum:'0',
            reInspection:'',//2024.12.2新加字段复验期
            reInspectionNoticeDay:'',//2024.12.2新加字段复验期提醒天数
            name: "",
            code: "",
            materialType: "",
            type: "成品",
            validDay: "",
            inventoryType:'',
            materialName:'',
            img: "",
            state: true,
            standard:"国标",
            dayRequirement: "",
            bomNum: "",
            stockConfig:[
                {
                    start: "",
                    end: "",
                    level: "高",
                },
                {
                    start: "",
                    end: "",
                    level: "中",
                },
                {
                    start: "",
                    end: "",
                    level: "低",
                },
            ],
            tqWay:[{value:''}],
            inputBatch:[{value:''}],
            tech:[{value:''}],
            notice:'',
            prodRecordCode:'备案中'
        },
        rules: {
            name: [{ required: true, message: "请输入名称", trigger: "blur" }],
            code: [{ required: true, message: "请输入编码", trigger: "blur" }],
            materialType: [{ required: true, message: "请选择产品分类", trigger: "change" }],
            validDay: [{ required: true, message: "请输入有效期", trigger: "blur" }],
            state: [{ required: true, message: "请选择状态", trigger: "change" }],
            type: [{ required: true, message: "请选择产品类别", trigger: "change" }],
            materialName: [{ required: true, message: "请选择投料", trigger: "change" }],
            bomNum: [{ required: true, message: "请输入投料需求", trigger: "blur" }],
            inventoryType: [{ required: true, message: "请选择产品大类", trigger: "change" }],
            spec: [{ required: true, message: "请选择产品规格", trigger: "change" }],
            loadingQuantity: [{ required: true, message: "请输入装量", trigger: "blur" }],
            planNum: [{ required: true, message: "请输入计划生产量", trigger: "blur" }],
            quarterSell: [{ required: true, message: "请输入计划包装量", trigger: "blur" }],
            standard: [{ required: true, message: "请选择产品标准", trigger: "change" }],
            stockConfig: [
                {
                    required: true,
                    validator: (rule, value, callback) => {
                        value.forEach(item=>{
                            if(item.start && item.end){
                                return callback()
                            }else{
                                return callback(new Error('请完善库存区间'))
                            }
                        })
                    },
                    trigger: "change",
                },
            ],
            tqWay: [{
                required: true,
                validator: (rule, value, callback) => {
                    value.forEach(item=>{
                        if(item.value){
                            return callback()
                        }else{
                            return callback(new Error('请完善提取方式'))
                        }
                    })
                },
                trigger: "change",
            },],
            inputBatch: [{
                required: true,
                validator: (rule, value, callback) => {
                    value.forEach(item=>{
                        if(item.value){
                            return callback()
                        }else{
                            return callback(new Error('请完善投料批次'))
                        }
                    })
                },
                trigger: "change",
            },],
            tech: [{
                required: true,
                validator: (rule, value, callback) => {
                    value.forEach(item=>{
                        if(item.value){
                            return callback()
                        }else{
                            return callback(new Error('请完善工艺'))
                        }
                    })
                },
                trigger: "change",
            },],
        },
    };
    modalOk(){
        (this.$refs.formModel as any).validate(async (valid: boolean) => {
            if (!valid) return;
            this.modalOption.loading = true;
            try {
                let params = JSON.stringify(this.formOption.formData)
                let temp = JSON.parse(params)
                //由于产品管理数据查询需要根据materialType的值查询,但是投料又需要对应materialName来查询所有又要将materialType转换回来
                if(temp.materialType == '中药材'){
                    temp.materialType = '饮片'
                }else if(temp.materialType == '饮片'){
                    temp.materialType = '提取物'
                }else if(temp.materialType == '提取物'){
                    temp.materialType = '配方颗粒(颗粒)'
                }else{
                    temp.materialType = '配方颗粒'
                }
                temp.materialId = this.materialList.find(item=>item.name === temp.materialName).id
                if(!temp.materialId){
                    this.$message.error('未找到投料')
                    return
                }
                temp.stockConfig = JSON.stringify(temp.stockConfig)
                temp.tqWay = JSON.stringify(temp.tqWay)
                temp.inputBatch = JSON.stringify(temp.inputBatch)
                temp.tech = JSON.stringify(temp.tech)
                temp.bomNum = temp.bomNum*0.01

                if(this.modalOption.title === "新增"){
                    const res = await nextApi.addMaterial(temp)
                    if(res.code === 200){
                        this.$message.success(res.msg)
                    }
                }else if(this.modalOption.title === "编辑"){
                    const res = await nextApi.editMaterial(temp)
                    if(res.code === 200){
                        this.$message.success(res.msg)
                    }
                }

            }finally {
                this.modalOption.loading = false;
                this.closeModal();
            }
        })
    }


    /**
     * @description 处理一些数据的方法
     *  **/
    //切换表单验证 如果是成品将表单验证的stockConfig字段设置为必填
    changeRule(){
        console.log(this.formOption.formData.type)
        if(this.formOption.formData.type === '成品'){
            this.formOption.rules = {
                name: [{ required: true, message: "请输入名称", trigger: "blur" }],
                code: [{ required: true, message: "请输入编码", trigger: "blur" }],
                materialType: [{ required: true, message: "请选择产品分类", trigger: "change" }],
                validDay: [{ required: true, message: "请输入有效期", trigger: "blur" }],
                state: [{ required: true, message: "请选择状态", trigger: "change" }],
                type: [{ required: true, message: "请选择产品类别", trigger: "change" }],
                materialName: [{ required: true, message: "请选择投料", trigger: "change" }],
                bomNum: [{ required: true, message: "请输入投料需求", trigger: "blur" }],
                inventoryType: [{ required: true, message: "请选择产品大类", trigger: "change" }],
                spec: [{ required: true, message: "请选择产品规格", trigger: "change" }],
                loadingQuantity: [{ required: true, message: "请输入装量", trigger: "blur" }],
                planNum: [{ required: true, message: "请输入计划生产量", trigger: "blur" }],
                quarterSell: [{ required: true, message: "请输入计划包装量", trigger: "blur" }],
                standard: [{ required: true, message: "请选择产品标准", trigger: "change" }],
                stockConfig: [
                    {
                        required: true,
                        validator: (rule, value, callback) => {
                            value.forEach(item=>{
                                if(item.start && item.end){
                                    return callback()
                                }else{
                                    return callback(new Error('请完善库存区间'))
                                }
                            })
                        },
                        trigger: "change",
                    },
                ],
                tqWay: [{
                    required: true,
                    validator: (rule, value, callback) => {
                        value.forEach(item=>{
                            if(item.value){
                                return callback()
                            }else{
                                return callback(new Error('请完善提取方式'))
                            }
                        })
                    },
                    trigger: "change",
                },],
                inputBatch: [{
                    required: true,
                    validator: (rule, value, callback) => {
                        value.forEach(item=>{
                            if(item.value){
                                return callback()
                            }else{
                                return callback(new Error('请完善投料批次'))
                            }
                        })
                    },
                    trigger: "change",
                },],
                tech: [{
                    required: true,
                    validator: (rule, value, callback) => {
                        value.forEach(item=>{
                            if(item.value){
                                return callback()
                            }else{
                                return callback(new Error('请完善工艺'))
                            }
                        })
                    },
                    trigger: "change",
                },],
            }
        }else{
            this.formOption.rules = {
                name: [{ required: true, message: "请输入名称", trigger: "blur" }],
                code: [{ required: true, message: "请输入编码", trigger: "blur" }],
                materialType: [{ required: true, message: "请选择产品分类", trigger: "change" }],
                validDay: [{ required: true, message: "请输入有效期", trigger: "blur" }],
                state: [{ required: true, message: "请选择状态", trigger: "change" }],
                type: [{ required: true, message: "请选择产品类别", trigger: "change" }],
                materialName: [{ required: true, message: "请选择投料", trigger: "change" }],
                bomNum: [{ required: true, message: "请输入投料需求", trigger: "blur" }],
                inventoryType: [{ required: true, message: "请选择产品大类", trigger: "change" }],
                spec: [{ required: true, message: "请选择产品规格", trigger: "change" }],
                loadingQuantity: [{ required: true, message: "请输入装量", trigger: "blur" }],
                planNum: [{ required: true, message: "请输入计划生产量", trigger: "blur" }],
                quarterSell: [{ required: true, message: "请输入计划包装量", trigger: "blur" }],
                standard: [{ required: true, message: "请选择产品标准", trigger: "change" }],
                tqWay: [{
                    required: true,
                    validator: (rule, value, callback) => {
                        value.forEach(item=>{
                            if(item.value){
                                return callback()
                            }else{
                                return callback(new Error('请完善提取方式'))
                            }
                        })
                    },
                    trigger: "change",
                },],
                inputBatch: [{
                    required: true,
                    validator: (rule, value, callback) => {
                        value.forEach(item=>{
                            if(item.value){
                                return callback()
                            }else{
                                return callback(new Error('请完善投料批次'))
                            }
                        })
                    },
                    trigger: "change",
                },],
                tech: [{
                    required: true,
                    validator: (rule, value, callback) => {
                        value.forEach(item=>{
                            if(item.value){
                                return callback()
                            }else{
                                return callback(new Error('请完善工艺'))
                            }
                        })
                    },
                    trigger: "change",
                },],
            }
        }
    }
    async getInfo(){
        const res = await nextApi.selectDetailById({id:this.editId})
        this.formOption.formData = res.data
        if(this.formOption.formData.stockConfig !== ""){
            this.formOption.formData.stockConfig = JSON.parse(res.data.stockConfig)
            if(this.formOption.formData.stockConfig.length === 0){
                this.formOption.formData.stockConfig = [
                    {
                        start: "",
                        end: "",
                        level: "高",
                    },
                    {
                        start: "",
                        end: "",
                        level: "中",
                    },
                    {
                        start: "",
                        end: "",
                        level: "低",
                    },
                ]
            }
            this.formOption.formData.tqWay = JSON.parse(res.data.tqWay)
            this.formOption.formData.inputBatch = JSON.parse(res.data.inputBatch) || [{value:''}]
            this.formOption.formData.tech = JSON.parse(res.data.tech)
            this.formOption.formData.bomNum = this.formOption.formData.bomNum*100
            //由于存进去的又是转换回去的materialType,这里根据ID查询的数据又必须转回对应关系
            if(this.formOption.formData.materialType == '配方颗粒'){
                this.formOption.formData.materialType = '配方颗粒(颗粒)'
            }else if(this.formOption.formData.materialType == '配方颗粒(颗粒)'){
                this.formOption.formData.materialType = '提取物'
            }else if(this.formOption.formData.materialType == '提取物'){
                this.formOption.formData.materialType = '饮片'
            }else{
                this.formOption.formData.materialType = '中药材'
            }

        }
        if(this.formOption.formData.materialType){
            await this.getMaterialList()
        }
    }
    standardChange(){
        this.formOption.formData.inventoryType = ''
        this.getMaterialList()
    }
    addTech(){
        this.formOption.formData.tech.push({value:''})
    }
    deleteTech(index){
        this.formOption.formData.tech.splice(index,1)
    }
    addTqWay(){
        this.formOption.formData.tqWay.push({value:''})
    }
    deleteTqWay(index){
        this.formOption.formData.tqWay.splice(index,1)
    }
    addInputBatch(){
        this.formOption.formData.inputBatch.push({value:''})
    }
    deleteInputBatch(index){
        this.formOption.formData.inputBatch.splice(index,1)
    }
    calcDayRequirement() {
        this.formOption.formData.dayRequirement = (this.formOption.formData.quarterSell / 90).toFixed(2)
        this.calcDay()
    }
    calcDay(){
        let quarterSell = this.formOption.formData.quarterSell
        if (this.formOption.formData.spec == '小袋') {
            if (quarterSell < 200000) {
                this.formOption.formData.packDay = 900
            } else if (quarterSell >= 200000 && quarterSell < 300000) {
                this.formOption.formData.packDay = 540
            } else if (quarterSell >= 300000) {
                this.formOption.formData.packDay = 365
            }
        }else if(this.formOption.formData.spec == '大袋'){
            if(quarterSell < 10000){
                this.formOption.formData.packDay = 900
            }else if(quarterSell >= 10000 && quarterSell < 30000){
                this.formOption.formData.packDay = 540
            }else if(quarterSell >= 30000){
                this.formOption.formData.packDay = 365
            }
        }
    }
    //产品类型change
    typeChange(v){
        this.formOption.formData.materialType = ''
        this.formOption.formData.standard = ''
    }
    materialTypeChange(){
        if(this.formOption.formData.materialType === '配方颗粒(颗粒)'){
            this.formOption.formData.bomNum = this.formOption.formData.bomNum || '99'
        }else if(this.formOption.formData.materialType === '提取物'){
            this.formOption.formData.bomNum = this.formOption.formData.bomNum || '97'
        }
        this.getMaterialList()
        console.log(this.formOption.formData);


    }
    //查询选择投料下拉列表
    materialList:any[] = []
    async getMaterialList(){
        let params = {
            materialType:this.formOption.formData.materialType,
            standard:this.formOption.formData.standard //2025年5/26 润泽说投料从来没有用过标准来查询 2025年7月7日 润泽说又要加上这个标准条件
        }
        const res = await nextApi.selectMaterialListByType(params)
        this.materialList = res.data
    }

    /**
     * @description 图片上传器回调
     * **/
    imgUpload(img:any){
        img.length ? this.formOption.formData.img = img[0].url:this.formOption.formData.img = ''
    }

    closeModal() {
        this.$emit("closeModal");
    }
    get investmentLabel(){
        let type = this.formOption.formData.materialType
        if(type === '配方颗粒'){
            return '计划包装量*装量/'
        }else if(type === '配方颗粒(颗粒)'){
            return '配方颗粒(颗粒)需求量/'
        }else if(type === '提取物'){
            return '提取物需求量/制成量'
        }else{
            return '提取饮片需求量/饮片收率'
        }
    }
}
</script>
<style scoped lang="scss">
.container {
    padding: 0;
    .MR-10 {
        margin-right: 10px;
    }
    .inputs {
        width: 200px;
    }
    .tool-bar {
        margin-bottom: 20px;

    }
    .table {
        margin: 20px 0;
    }
    .col-h{
        height: 2.4rem;
    }
    .process-next{
        font-size: 26px;
        margin: 0 30px;
    }
}
</style>
